package com.hanlin.backend.controller;


import com.hanlin.backend.common.annotation.CodeDate;
import com.hanlin.backend.common.entity.HlAuthority;
import com.hanlin.backend.common.entity.HlUser;
import com.hanlin.backend.common.util.ResultCommon;
import com.hanlin.backend.dto.AuthorityDto;
import com.hanlin.backend.service.HlAuthorityService;
import com.hanlin.backend.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author hl.yuan
 * @since 2021-01-06
 */
@RestController
@Slf4j
@Api(value = "/frontend/api/authority", tags = "权限")
public class HlAuthorityController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private HlAuthorityService hlAuthorityService;

    @CodeDate(title = "标题")
    @GetMapping("/frontend/api/authority/getUserAuthority")
    @ApiOperation("获取当前用户的菜单权限列表，用于登录后，展示菜单")
    public ResultCommon<List<AuthorityDto>> getUserAuthority(HttpServletRequest request){
        ResultCommon resultCommon = new ResultCommon();
        try {
            HlUser hlUser = tokenService.getUserByRequest(request);
            List<AuthorityDto> list = hlAuthorityService.getUserAuthority(hlUser.getId());
            resultCommon.setData(list);
        }catch (Exception ex) {
            resultCommon.setCode("500");
            log.error("菜单权限列表获取失败",ex);
        }

        return resultCommon;
    }

    @GetMapping("/frontend/api/authority/getAuthorityList")
    @ApiOperation("根据userID或者roleId获取权限列表")
    public ResultCommon<List<AuthorityDto>> getAuthorityList(Integer userId,Integer roleId){
        ResultCommon resultCommon = new ResultCommon();
        try {
            List<AuthorityDto> list = hlAuthorityService.getAuthorityList(userId,roleId);
            resultCommon.setData(list);
        }catch (Exception ex) {
            resultCommon.setCode("500");
            log.error("菜单权限列表获取失败",ex);
        }

        return resultCommon;
    }

    @GetMapping("/frontend/api/authority/getAuthorityAll")
    @ApiOperation("获取所有菜单列表")
    public ResultCommon<List<AuthorityDto>> getAuthorityAll(boolean showRoot){
        ResultCommon resultCommon = new ResultCommon();
        try {
            List<AuthorityDto> list = hlAuthorityService.getAuthorityAll(showRoot);
            resultCommon.setData(list);
        }catch (Exception ex) {
            resultCommon.setCode("500");
            log.error("获取所有菜单列表",ex);
        }

        return resultCommon;
    }


    @PostMapping("/frontend/api/authority/saveAuthority")
    @ApiOperation("保存菜单列表")
    public ResultCommon saveAuthority(@RequestBody Map<String,Object> param){
        ResultCommon resultCommon = new ResultCommon();
        try {
            List<Map> listAuthority = (List<Map>) param.get("listAuthority");
            Integer userId = (Integer) param.get("userId");
            Integer roleId = (Integer) param.get("roleId");
            hlAuthorityService.saveAuthority(listAuthority,userId,roleId);
        } catch (Exception ex){
            resultCommon.setCode("500");
            log.error("保存菜单列表失败",ex);
        }
        return resultCommon;
    }

    @PostMapping("/frontend/api/authority/saveMenu")
    @ApiOperation("保存菜单")
    public ResultCommon saveMenu(@RequestBody HlAuthority authority){
        ResultCommon resultCommon = new ResultCommon();
        try {
            hlAuthorityService.saveMenu(authority);
        } catch (Exception ex){
            resultCommon.setCode("500");
            log.error("保存菜单失败",ex);
        }
        return resultCommon;
    }

    @GetMapping("/frontend/api/authority/deleteMenu")
    @ApiOperation("删除菜单")
    public ResultCommon deleteMenu(Integer id){
        ResultCommon resultCommon = new ResultCommon();
        try {
            hlAuthorityService.deleteMenu(id);
        } catch (Exception ex){
            resultCommon.setCode("500");
            log.error("删除菜单失败",ex);
        }
        return resultCommon;
    }

    @GetMapping("/frontend/api/authority/getMenu")
    @ApiOperation("根据id获取菜单")
    public ResultCommon<HlAuthority> getMenu(Integer id){
        ResultCommon resultCommon = new ResultCommon();
        try {
            HlAuthority menu = hlAuthorityService.getMenu(id);
            resultCommon.setData(menu);
        } catch (Exception ex){
            resultCommon.setCode("500");
            log.error("根据id菜单失败",ex);
        }
        return resultCommon;
    }



}

